<?php $this->jQuery()->enable()?>
<?php $this->JQuery()->javascriptCaptureStart()?>
jQuery(function() {
	 
	$(document).ready(function() {
		$j('.pricerange').html('<label class="optional" for="key_search_tools-price"><?php echo $this->t('Price')?></label><span class="low"><input type="text" class="text-range" id="key_search_tools-price-0" name="key_search_tools-price"></span><span> - </span><span class="high"><input type="text" class="text-range" id="key_search_tools-price-1" name="key_search_tools-price-1"></span>');
		
	<?php if(!empty($_GET['key_search_tools']['small_category_list'])){?>
		$("#key_search_tools-small_category_list option[value='<?php echo $_GET['key_search_tools']['small_category_list'];?>']").attr('selected', 'selected');
	<?php }?>
	
	<?php if(!empty($_GET['key_search_tools']['area_l2'])){?>
		$("#key_search_tools-area_l2 option[value='<?php echo $_GET['key_search_tools']['area_l2'];?>']").attr('selected', 'selected');
	<?php }?>
	
	<?php if(!empty($_GET['key_search_tools-price'])){?>
		$("#key_search_tools-price-0").val('<?php echo $_GET['key_search_tools-price'];?>');
	<?php }?>
	
	<?php if(!empty($_GET['key_search_tools-price-1'])){?>
		$("#key_search_tools-price-1").val('<?php echo $_GET['key_search_tools-price-1'];?>');
	<?php }?>
	});
});
<?php $this->JQuery()->javascriptCaptureEnd()?>
    <section class="serp">
	    <h2>
			<?php if(!empty($_GET['keyword']) || !empty($_GET['key_search_tools']['keywords'])):?>
				<?php echo ($this->listing_number>0)? $this->t('Ads for "<em>__1__</em>"', null, $_GET['keyword'] ): $this->t('No Ads for "<em>__1__</em>"', null, $_GET['keyword'] ) ?>
	           <!-- <?php echo ($this->listing_number>0)?$this->t('Ads for'):$this->t('No Ads for')?> "<em><?php echo !empty($_GET['keyword']) ? $_GET['keyword'] : ((!empty($_GET['key_search_tools']['keywords'])) ? $_GET['key_search_tools']['keywords'] : '');?><?php /*echo !empty($_GET['key_search_tools']['keywords']) ? $_GET['key_search_tools']['keywords'] : '';*/?></em>"-->
	        <?php else:?>
	            <?php if (!empty($this->category)) {
	                $tmp = $this->category->getParentTree();
	                $to_add = '';
	                foreach((array)$tmp as $k => $cat):?>
	                    <?php if($k == 0) { continue;}?>
	                    
	                <?php endforeach;?>
					<?php echo ($this->listing_number>0)?'<em>'.$cat->name.'</em>':$this->t('No Ads for "<em>__1__</em>"', null, $cat->name )?>
	            <?php } ?>
	        <?php endif;?>
	    <?php if($this->listing_number>0):?>
		<!--span>
			<?php echo (($this->page-1) * $this->per_page == 0)? '1':($this->page-1) * $this->per_page?>-<?php echo (($this->listing_number)<29)?$this->listing_number:($this->page * $this->per_page)?> / <b><?php echo number_format($this->listing_number)?> Ads</b>, Page <?php echo number_format($this->page)?> / <?php echo number_format(ceil($this->listing_number/$this->per_page))?>
		</span-->
		<span>
			<?php echo $this->t('__1__-__2__ /__3__ __4__ Ads__5__, Page __6__ / __7__', null, ((($this->page-1) * $this->per_page == 0)? '1':($this->page-1) * $this->per_page), ((($this->listing_number)<29)?$this->listing_number:($this->page * $this->per_page)), '<b>', number_format($this->listing_number), "</b>", number_format($this->page), number_format(ceil($this->listing_number/$this->per_page)))?>
			<!--<?php echo (($this->page-1) * $this->per_page == 0)? '1':($this->page-1) * $this->per_page?>-<?php echo (($this->listing_number)<29)?$this->listing_number:($this->page * $this->per_page)?> / <b><?php echo number_format($this->listing_number)?> Ads</b>, Page <?php echo number_format($this->page)?> / <?php echo number_format(ceil($this->listing_number/$this->per_page))?>-->
		</span>
		<?php endif;?>
		</h2>
		
<?php 
if($this->listing_number > 0){
?>
		<?php 
//next and prev url settter
$url_product = array();
function pager($cur_page_num, $url_raw, $pages){
	$url_product = array();
	$prev = ($cur_page_num>1)? "&page=".($cur_page_num-1):'';
	$next = ($cur_page_num<$pages)? "&page=".($cur_page_num+1):'';
	
	if(strpos($url_raw, '?')===false)
	{
		$url_product['prev'] = $url_product['next'] = $url_raw;
		$url_product['prev'] .= ($cur_page_num>1)? str_replace("&","?",$prev):'';
		$url_product['next'] .= ($cur_page_num<$pages)? str_replace("&","?",$next):'';
	}else
	{
		if(preg_match('/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/i', $url_raw))
		{
			$url_product['prev'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$prev), $url_raw);
			$url_product['next'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$next), $url_raw);
		}else
		{
			$url_product['prev'] = $url_raw.$prev;
			$url_product['next'] = $url_raw.$next;
			
		}
	}
	return $url_product;
}		
		
		$pager_url = pager($this->page, $_SERVER["REQUEST_URI"], ceil($this->listing_number/$this->per_page));
		//print_r($pager_url);
		$previous_url = $pager_url['prev'];
		$next_url = $pager_url['next']; 
		//$previous=($this->page>1)? "?page=".($this->page-1):'';
		//$next=($this->page<ceil($this->listing_number/$this->per_page))? "?page=".($this->page+1):'';
		?>
	    <div class="nav top">
	    <a href="<?php echo $previous_url;?>" class="prev"><span>&lt;</span><?php echo $this->t('PREV')?></a>
	    <form action="/search/regular" method="get" id="form_sort1">
            <?php
            foreach($_GET as $k => $v){
                if (is_array($v)) {
                    foreach($v as $k2 => $v2) {
                        if ($k2 != 'sort') {
                            ?>
                            <input type="hidden" name="<?php echo $k.'['.$k2.']'?>" value="<?php echo $v2?>">
                            <?php
                        }
                    }
                }else {
                    if ($k != 'sort') {
                        ?>
                        <input type="hidden" name="<?php echo $k?>" value="<?php echo $v?>">
                        <?php
                    }
                }
            }
            ?>

            <select name="sort" onchange="javascript:if(this.value!='default'){$('#form_sort1').submit();}">
            	<option value="default"><?php echo $this->t('Sort By...')?></option>
                <option value="recent" <?php echo !empty($this->sort) && $this->sort == 'recent' ? 'selected' : ''?>><?php echo $this->t('Most Recent')?></option>
                <option value="rel" <?php echo empty($this->sort) || $this->sort == 'rel' ? 'selected' : ''?>><?php echo $this->t('Relevance')?></option>
                <option value="highest" <?php echo !empty($this->sort) && $this->sort == 'highest' ? 'selected' : ''?>><?php echo $this->t('Highest Price')?></option>
                <option value="lowest" <?php echo !empty($this->sort) && $this->sort == 'lowest' ? 'selected' : ''?>><?php echo $this->t('Lowest Price')?></option>
            </select>
        </form>
	    <a href="<?php echo $next_url;?>" class="next"><?php echo $this->t('NEXT')?><span>&gt;</span></a>
	    </div>
	    <ul>
	    <?php $i = 0;?>
            <?php foreach($this->list as $listing):?>
            	<?php $listing_url = $listing->url[$this->getLanguagePosition()]?>
            	<?php //$listing_url = str_replace(array('/en', '/tc'), '/'. $this->current_language, $listing->url['0'])?>
            	<?php
				if(!empty($listing_url)):?>
            	<li>
            		<?php if ($listing->type_obj == Model_Company::TYPE): ?>
            		
                        <?php $listing_url = str_replace(array('/en', '/tc'), '/'. $this->current_language, $listing->url)?>
                        <a href="<?php echo $listing_url.'?sid='.$this->search_id?>">
	                        <figure>                            
	                            <img src="<?php echo $this->thumbnail()->scale($listing->photo, 82, 82);?>" alt="<?php echo $this->t('__1__ - photo __2__', null, $listing->title, 1)?>">
	                        </figure>
	                        <h1><?php echo $listing->username?></h1>
	                        <dl class="location">
	                            <?php if(!empty($listing->website)):?>
	                                <dd><?php echo $this->t('Website: __1__', null, $listing->website);?></dd>
	                            <?php endif;?>
	                        </dl>
	                        <?php if (!empty($listing->mailing_address)):?>
	                        <dl class="price">
                                <dd><?php echo $listing->mailing_address;?></dd>
                        	</dl>
                            <?php endif;?>
                        </a>
                    <?php else: ?>
					<a href="<?php echo $listing_url.'?sid='.$this->search_id?>">
						<figure>	
							<img src="<?php echo $this->thumbnail()->scale($listing->photo, 82, 82);?>" alt="<?php echo $this->t('__1__ - photo __2__', null, $listing->title, 1)?>">
						</figure>
						<h1><?php echo $listing->title;//$listing->username?>
						<?php if($listing->sold):?>
                            <?php $tmp_cat = (array)$listing->category_attached; ?>
                            <span class="sold"><?php $tmp = Model_Category::find($tmp_cat[0]); echo $tmp->sold;?></span>
                        <?php endif;?></h1>
						<dl class="location">
                            <?php if(!empty($listing->area_l2)):?>
                                <dd><?php echo Model_Location::find($listing->area_l2)->name?></dd>
                            <?php endif;?>
                        </dl>
						<dl class="date">
							<dd><?php echo date(MEDIUM_DATE_FORMAT, strtotime($listing->posted_at))?></dd>
						</dl>
						<dl class="price">
							<dd>
								<?php if($listing->sold):?>
									<?php echo $this->t('Sold')?>
								<?php else:?>
									<?php if ($i==0):?><dt><span>Price</span></dt><?php endif;?>
		                            <?php if (!empty($listing->sale_price) || !empty($listing->rent_price)): ?>
		                                <?php
		                                if (!empty($listing->sale_price)) {
		                                    $sale_price = number_format($listing->sale_price);
		                                    ?><dd>HKD <?php echo $sale_price?> <em> <?php echo $this->t('(Sale)')?></em></dd><?php
		                                }
		                                if (!empty($listing->rent_price)) {
		                                    $rent_price = number_format($listing->rent_price);
		                                    ?><dd>HKD <?php echo $rent_price?> <em> <?php echo $this->t('(Rent)')?></em></dd><?php
		                                }
		                                ?>   
		                                    
		                            <?php elseif (!empty($listing->is_free)):?>
		                                <dd><?php echo $this->t('Free');?></dd>
		                            <?php elseif (!empty($listing->is_contact)):?>
		                                <dd><?php echo $this->t('Please contact');?></dd>
		                            <?php elseif (!empty($listing->price)): ?>
		                                <dd>HKD <?php echo number_format($listing->price);?></dd>
		                            <?php endif;?>
	                            <?php endif;?>
							</dd>
						</dl>
						<span>&gt;</span>
					</a>
					<?php endif; ?>
				</li>
                <?php $i++;?>
                <?php endif;?>
            <?php endforeach;?>
	    </ul>
	    <div class="nav bottom">
	    <a href="<?php echo $previous_url;?>" class="prev"><span>&lt;</span><?php echo $this->t('PREV')?></a>
	    <p><?php echo $this->t('Page __1__ / __2__', null, number_format($this->page), number_format(ceil($this->listing_number/$this->per_page)))?></p>
		<a href="<?php echo $next_url;?>" class="next"><?php echo $this->t('NEXT')?><span>&gt;</span></a>
	    </div>
<?php }else{
?>
	<?php if(empty($_GET['small_category_list'])){?>
		<?php if (empty($_GET['keyword'])){ ?>
	       	<p>
		   		<a href="/listing/post-an-ad"><?php echo $this->t('Post a wanted Ad')?></a> 
			   <?php echo $this->t('and receive messages from interested people')?> 
			</p>
	
	    <?php } else{?>
	    	<?php if(isset($_GET['key_search_tools']) && (isset($_GET['key_search_tools']['small_category_list']) || !empty($_GET['key_search_tools']['small_category_list']) || $_GET['key_search_tools']['small_category_list']!='')){?>
	    		<p> 
				   <?php echo $this->t('Try expanding your search or narrow using another keyword.')?> 
				</p>
	    	<?php }else{?>
		    	<ol>
		            <li><?php echo $this->t('Did you spell the keywords correctly?');?></li>
		            <li><?php echo $this->t('Try another similar keyword.');?></li>
		            <li><?php echo $this->t('Try browsing by');?> <a href="/category/view-all"><?php echo $this->t('category')?></a></li>
		        </ol>   
			<?php }?>    
	    <?php } ?>
	<?php }else{
			if(isset($_GET['key_search_tools']) && empty($_GET['keyword']) && !empty($_GET['small_category_list'])){
				$cat = Model_Category::find($_GET['small_category_list']);
				echo "<h2>".$this->t('No Ads for __1__', null, '"<em>'.$cat->name.'</em>"' )."</h2>";
			}
	?>
			
			<p>
	            <?php echo $this->t('Try browsing another');?> <a href="/category/view-all"><?php echo $this->t('category')?></a> <?php echo $this->t('or try a keyword search.');?>
	        </p> 
    <?php }?>
<?php	



}?>
    </section>
<?php if($this->listing_number>0):?>
    <section class="advanced_search">
    
		<h2><?php echo $this->t('Narrow Your Search');?></h2>
    	<?php 
    		echo $this->form2;
    	?>
    

    </section>
<?php endif;?>
    <?php echo $this->Partial('mobile/other.phtml', 'default'); ?>


